package src.leetcode;

import src.tree.TreeNode;

import java.util.*;

public class Main {
    public static void main(String[] args) {
//        List<Integer> list = new ArrayList<>(Arrays.asList(1,2,3,4,5,6,7,8,9,10));
//        TreeNode root = TreeNode.createTree(list);
//        //TreeNode.printTree(root);
//        StringBuilder stringBuilder = new StringBuilder();
//        Solution solution = new Solution();
//        System.out.println(solution.countNodes(root));

//        System.out.println(2 ^ 7 ^ 12 ^ 6 ^ 4);
        int[] num1 = {1,2,3,4,5,6,7,8,9,10};
        int[] num2 = new int[num1.length];
        System.out.println(Arrays.toString(num1));
        System.out.println(Arrays.toString(num2));
        num2 = num1;
        System.out.println(Arrays.toString(num2));
        num2[0] = 100;
        System.out.println(Arrays.toString(num1));
        System.out.println(Arrays.toString(num2));
    }
}

class Dog {
    int head;
    Integer age;
}

class Solution {
    public int countNodes(TreeNode root) {
        int dl = countdeepl(root);
        int dr = countdeepr(root);
        if (dr == dl) return (int) Math.pow(2, dl) - 1;

        int left = 0, right = (int) Math.pow(2, dl - 1);
        while (left < right) {
            int mid = left + (right - left) / 2;
            if (findNode(root, dl, mid)) {
                left = mid + 1;
            }else {
                right = mid;
            }
        }

        return (int) Math.pow(2, dr) - 1 + left;
    }

    private boolean findNode(TreeNode root, int deep, int mid) {
        TreeNode node = root;
        int movenums = deep - 1;
        for (;movenums > 0; movenums--) {
            if ((mid >> (movenums - 1)) % 2 == 0) node = node.left;
            else node = node.right;
        }
        return node != null;
    }

    private int countdeepl(TreeNode root) {
        int res = 0;
        TreeNode node = root;
        while (node != null) {
            node = node.left;
            res++;
        }
        return res;
    }
    private int countdeepr(TreeNode root) {
        int res = 0;
        TreeNode node = root;
        while (node != null) {
            node = node.right;
            res++;
        }
        return res;
    }

}